package in.muton.edushare.components.forms.univsettings;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.api.Authentication;
import in.muton.edushare.wicket.converters.choicerenderers.GenericRenderer;

import java.util.HashMap;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Universities;

public class UnivComm extends Panel {

	public UnivComm(String id, IModel<Universities> aModel) {
		super(id, aModel);
		model = aModel;

		auth = new Authentication(model.getObject(), EduShareSession.get()
				.getLoggedInUser());
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("University administrators", Authentication.ADMINS);
		hm.put("University Teachers and Administrators",
				Authentication.ADMIN_TEACHERS);
		hm.put("Entire University", Authentication.ALL_UNIV);

		final GenericRenderer<String> gr1 = new GenericRenderer<String>(hm, auth
				.getSetting(Authentication.MSG_SND));
		final GenericRenderer<String> gr2 = new GenericRenderer<String>(hm, auth
				.getSetting(Authentication.CAL_NEW));
		final GenericRenderer<String> gr3 = new GenericRenderer<String>(hm, auth
				.getSetting(Authentication.FILE_UP));

		Form<Void> f = new Form<Void>("univComm") {
			@Override
			public void onSubmit() {

				auth.changeSetting(Authentication.MSG_SND, gr1.getSelection());
				auth.changeSetting(Authentication.CAL_NEW, gr2.getSelection());
				auth.changeSetting(Authentication.FILE_UP, gr3.getSelection());
				
				model.getObject().setSettings(auth.getString());
				EduShareSession.get().getSessionORMInterface().commitAllChanges();
			}

			private static final long serialVersionUID = -1762150476942878673L;
		};

		add(f);
		f.add(new DropDownChoice<String>("msgPriv", gr1.getModel(), gr1.getOptions(), gr1));
		f.add(new DropDownChoice<String>("schPriv", gr2.getModel(), gr2.getOptions(), gr2));
		f.add(new DropDownChoice<String>("fuPriv", gr3.getModel(), gr3.getOptions(), gr3));		
	}

	private IModel<Universities> model;
	private Authentication auth = null;
	private static final long serialVersionUID = 5783939969181164086L;
}
